<?php include '../../authorization_check.php' ?>
<?php include '../../checkSecretaryOnly.php' ?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-9">
<title>Define New Course</title>
<SCRIPT TYPE="text/javascript" src= "../../commonutils/util/util.js"> </SCRIPT>
</head>
<style type="text/css">
/* th {text-align: right} */
</style>
<body>

<?php include 'top.html' ?>

<?php
  session_start();
  $programCode = $_SESSION['programCode'];
  $programShortName = $_SESSION['programShortName'];
  $semesterID = $_SESSION['semesterID'];
  
  echo " <p>You are currently working in <font color=#00008B> <b>".$programShortName."</b> </font> program. </p>"; 
  
  echo " <hr color=blue>";
?>

<?php
  require_once '../../commonutils/db/DBConnectionManager.php';
  $numerrors = 0;
  if(isset($_POST['btnSubmit'])){
	$courseCode = $programCode.$_POST['txtCode'];
	$courseName = strtoupper($_POST['txtName']); 
	$credit = $_POST['credit'];
	$description = $_POST['dsc']; 	
  	echo "<p><b><font color=red> ";

  	if(strlen($courseCode) < 7){
  		$numerrors = $numerrors + 1;
  		echo "* Course Code must be a three digit integer and can not be left empty. </br>";
  	}
  	if(strlen($courseName) < 1){
  		$numerrors = $numerrors + 1;
  		echo "* Course Name can not be left empty. <br>";
  	}
  	$sqlquery = " select count(*) as cnt from course where courseCode = ".$courseCode;
    //echo $sqlquery."<br>";
  	$result = DBConnectionManager::query($sqlquery);
  	$row = mysql_fetch_assoc($result);
  	$cnt = $row['cnt'];
  	//echo $cnt."<br>";
  	if($cnt > 0){
  		$numerrors = $numerrors + 1;
  		echo "* There is an already defined course with the code: ".$courseCode.". <br>";
  	}  

  	$sqlquery = " select count(*) as cnt from course where courseName = '".$courseName."'";
    //echo $sqlquery."<br>";
  	$result = DBConnectionManager::query($sqlquery);
  	$row = mysql_fetch_assoc($result);
  	$cnt = $row['cnt'];
  	//echo $cnt."<br>";
  	if($cnt > 0){
  		$numerrors = $numerrors + 1;
  		echo "* There is an already defined course with the name: ".$courseName." <br>";
  	}  
  	
  	
  	echo "</font> </b></p>";	
  	
    
	//echo "<p>".$numerrors."</p>";
	if ($numerrors == 0)
	{
		
		$transaction = " INSERT INTO course (programCode, courseCode, courseName, credit, description)".
					   " VALUES (".$programCode.",".$courseCode.", '".$courseName."' , ".$credit.", '".$description."')";
		DBConnectionManager::query($transaction);
		//echo $transaction."<br>";
		
		echo "<p><font color=blue><b> You have successfully added the ".$courseName." course to the system. </b></font> </p>";
		echo "<p><a href=ViewDefinedCourses.php> View defined courses. </a></p>";
	}
	  	
  }
?>
  
<FORM NAME ="form1" METHOD ="POST" ACTION = "DefineNewCourse.php">
<table>

<tr>
<th>Course Code(3 digit number):</th>
<td><input type="text" name="txtCode" size="5" onKeyPress="return numbersonly(this, event)" 
	value="<?php if($numerrors > 0) echo "".$_POST['txtCode']; ?>" maxlength="3"  ></td>
</tr>

<tr>
<th>Name:</th>
<td><input style="text-transform: uppercase" type="text" name="txtName" size="110"
	 value="<?php if($numerrors > 0) echo "".$courseName; ?>"></td>
</tr>

<tr>
<th>Credit:</th>
<td>
<select name="credit" >  
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3" selected>3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
</select>
</td>
</tr>

<tr>
<th>Brief description:</th>
<td><textarea name="dsc" rows="10" cols="80" ><?php if($numerrors > 0) echo "".$description; ?></textarea></td>
</tr>

</table>
<p style="margin-left:300">
<input type="submit" value="  Submit  " name="btnSubmit">
&nbsp;&nbsp;
<input type="reset" value="  Reset  " name="btnReset">
</p>
</FORM>

</p>  

<?php include 'bottom.php' ?>

</body>


</html>